package com.company.cloud.company.modules.mgr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.IdStrParam;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.company.modules.api.request.CompanyQueryRequest;
import com.company.cloud.company.modules.api.response.CompanyFull;
import com.company.cloud.company.modules.app.controller.req.CompanyQuery4PageParam;
import com.company.cloud.company.modules.base.entity.CompanyInfo;
import com.company.cloud.company.modules.base.entity.TenantApplyInfo;
import com.company.cloud.company.modules.base.service.TenantApplyInfoService;
import com.company.cloud.company.modules.cache.models.TenantApplyModel;
import com.company.cloud.company.modules.cache.utils.CompanyConvertUtils;
import com.company.cloud.company.modules.mgr.controller.req.TenantApplyCheckParam;
import com.company.cloud.company.modules.mgr.controller.req.TenantApplyQuery4PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.function.Function;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/user/companyApply")
@Api(tags="Company_App_公司申请")
public class TenantApplyMgrController {

    @Autowired
    private TenantApplyInfoService tenantApplyInfoService;

    @PostMapping("/query")
    @ApiOperation(value="公司申请查询", response= TenantApplyModel.class)
    public BaseResult query(@RequestBody TenantApplyQuery4PageParam params) {

        IPage<TenantApplyInfo> page =  tenantApplyInfoService.page(
                new Page<TenantApplyInfo>(params.getPage(), params.getRows()),
                new QueryWrapper<TenantApplyInfo>()
                        .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                        .like(StringUtils.hasText(params.getName()), "name", params.getName())
                        .like(StringUtils.hasText(params.getLinkName()), "link_name", params.getLinkName())
                        .eq(StringUtils.hasText(params.getLinkTel()), "link_tel", params.getLinkTel())
                        .eq(StringUtils.hasText(params.getLinkMail()), "link_mail", params.getLinkMail())
                        /*.eq(StringUtils.hasText(params.getBizValue1()), "biz_value1", params.getBizValue1())
                        .eq(StringUtils.hasText(params.getBizValue2()), "biz_value2", params.getBizValue2())
                        .eq(StringUtils.hasText(params.getBizValue3()), "biz_value3", params.getBizValue3())
                        .eq(StringUtils.hasText(params.getBizValue4()), "biz_value4", params.getBizValue4())
                        .eq(StringUtils.hasText(params.getBizValue5()), "biz_value5", params.getBizValue5())*/
                        .eq(StringUtils.hasText(params.getState()), "state", params.getState())
                        .orderByDesc("cdt")
        );


        IPage<TenantApplyModel> data=page.convert(new Function<TenantApplyInfo, TenantApplyModel>() {
            @Override
            public TenantApplyModel apply(TenantApplyInfo entity) {
                return CompanyConvertUtils.getTenantApplyModel(entity);
            }
        });
        return BaseResult.ok(data);
    }


    @PostMapping("/info")
    @ApiOperation(value="公司申请获取", response= TenantApplyModel.class)
    @StaffLogc(title = "Company_App_公司申请 、公司申请获取", action = "Company_App_公司申请、公司申请获取", actionAdo = "Company_App_公司申请")
    public BaseResult info(@RequestBody IdStrParam params) {
        ValidatorUtils.validate(params);
        TenantApplyInfo entity= tenantApplyInfoService.getById(params.getId());
        if (entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        TenantApplyModel data= CompanyConvertUtils.getTenantApplyModel(entity);
        return BaseResult.ok(data);
    }


    @PostMapping("/check")
    @StaffLogc(title = "Company_App_公司申请 、公司申请审核", action = "Company_App_公司申请、公司申请审核", actionAdo = "Company_App_公司申请")
    @ApiOperation(value="公司申请审核", response= BaseResult.class)
    public BaseResult check(@RequestBody TenantApplyCheckParam params) {
        ValidatorUtils.validate(params);
        TenantApplyInfo entity= tenantApplyInfoService.getById(params.getId());
        if (entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        if(entity.getState().equals(params.getState())){
            return BaseResult.ok();
        }
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        entity.setState(params.getState());
        entity.addEditParam(userInfo);
        tenantApplyInfoService.updateById(entity);
        return BaseResult.ok();
    }



}
